package main

import "fmt"

//面向对象实现计算器功能
func main() {
	obj := Add{Operate{
		num1: 100,
		num2: 200,
	}}

	obj2 :=Sub{Operate{
		num1: 1000,
		num2: 800,
	}}
	//[1] 通过对象方法调用实现加法运算
	result := obj.Result()
	fmt.Printf("通过对象方法调用实现加法运算：%d\n",result)

	//[2] 通过接口调用实现加法运算
	var opt Opter
	opt = &obj
	value := opt.Result()
	fmt.Printf("通过接口实现加法运算：%d\n",value)

	//[3] 通过多态实现计算器
	Result(&obj)
	Result(&obj2)
}

type Opter interface {
	Result() int
}
//父类
type Operate struct{
	num1 int
	num2 int
}

//子类
type Add struct {
	Operate
}

func (add *Add) Result() int {
	return add.num1 + add.num2
}

type Sub struct {
	Operate
}

func (sub *Sub) Result() int {
	return sub.num1 - sub.num2
}

func Result(opter Opter) {
	value := opter.Result()
	fmt.Printf("通过多态实现计算器功能：两数相加/相减 %d\n",value)
}
